package net.mingsoft.statistic.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.mingsoft.basic.util.DateUtil;
import net.mingsoft.cms.dao.IContentDao;
import net.mingsoft.cms.entity.ContentEntity;
import net.mingsoft.document.dao.FileDao;
import net.mingsoft.statistic.action.q.PubReportQuery;
import net.mingsoft.statistic.action.q.SummaryQuery;
import net.mingsoft.statistic.action.r.SummaryResponse;
import net.mingsoft.statistic.biz.SummaryBiz;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author gengfa
 * @date 22/02/14
 */
@Service
public class SummaryBizImpl implements SummaryBiz {

    @Resource
    FileDao fileDao;

    @Resource
    IContentDao contentDao;

    @Override
    public SummaryResponse summary(SummaryQuery query) {
        SummaryResponse response = new SummaryResponse();
        response.setDocumentCount(incrCount(query))
                .setDocumentAvgDayCount(incrCountAvg(query))
                .setReportPubCount(pubCount(query))
                .setReportPubAvgDayCount(pubCountAvg(query));
        return response;
    }

    @Override
    public long incrCount(SummaryQuery query) {
        return fileDao.incrCount(query);
    }

    @Override
    public long incrCountAvg(SummaryQuery query) {
        long days = DateUtil.days(DateUtil.toJodaDateTime(query.getBeginDate()),
                DateUtil.toJodaDateTime(query.getEndDate()));
        return fileDao.incrCountAvg(query) / days;
    }

    @Override
    public long pubCount(SummaryQuery query) {
        return fileDao.pubCount(query);
    }

    @Override
    public long pubCountAvg(SummaryQuery query) {
        long days = DateUtil.days(DateUtil.toJodaDateTime(query.getBeginDate()),
                DateUtil.toJodaDateTime(query.getEndDate()));
        return fileDao.pubCountAvg(query) / days;
    }

    @Override
    public Page<ContentEntity> getPubList(PubReportQuery q) {
        return contentDao.getPubContent(new Page<>(q.getPageNo(), q.getPageSize()), q);
    }
}
